<?php

require_once('functions/pageLoad.php');
loginRequired();
$user_session = explode("_", $_SESSION['user']);
$user_id = $user_session[0];

// find job and verify user

$sql = "SELECT jobs.id, area, trade, quotes.tradesman_id FROM jobs 
	LEFT JOIN quotes ON jobs.id = quotes.job_id 
	LEFT JOIN trades ON jobs.trade_id = trades.id 
	LEFT JOIN areas ON jobs.area_id = areas.id 
	WHERE jobs.user_id = '".$user_id."' AND quotes.id = '".mysql_real_escape_string($_GET['qid'])."'";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
$job_rows = mysql_num_rows($query);
$job_rs=mysql_fetch_assoc($query);

if($job_rows > 0){

$sql = "UPDATE jobs SET quote_id = '".mysql_real_escape_string($_GET['qid'])."', status = 'Closed', date_accepted = NOW() WHERE id = '".$job_rs['id']."'";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());

$sql = "UPDATE quotes SET status = 'Declined' WHERE job_id = '".$job_rs['id']."' AND id != '".mysql_real_escape_string($_GET['qid'])."'";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());

$sql = "UPDATE quotes SET status = 'Accepted' WHERE id = '".mysql_real_escape_string($_GET['qid'])."'";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());

// send email to tradesman

$job_link = build_job_link($job_rs['trade'],$job_rs['area'],$job_rs['id']);

$sql = "SELECT email, firstname, surname, mobile, mobile_verified, accepted_quotes FROM users 
	LEFT JOIN tradesman ON users.id = tradesman.user_id 
	WHERE tradesman.id = '".$job_rs['tradesman_id']."'";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
$user_rows = mysql_num_rows($query);
$user_rs=mysql_fetch_assoc($query);

require_once("classes/class.phpmailer.php");
	$mail = new PHPMailer();
	$mail->IsSMTP(); // telling the class to use SMTP
	$mail->Host = "localhost"; // SMTP server
	$mail->From = "mail@iwantatradesman.co.uk";
	$mail->FromName = "I Want A Tradesman";

	$mail->AddAddress($user_rs['email']);

	$mail->Subject = "Quote accepted";
	$mail->Body = "Dear ".$user_rs['firstname']." ".$user_rs['surname']."\r\n\r\nCongratulations! A user has accepted one of your quotations.\r\n\r\nYou can view the acceptance here: http://www.iwantatradesman.co.uk".$job_link."\r\n\r\nKind regards\r\n\r\nThe I Want A Tradesman Team\r\n\r\n\r\nThis email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.";

	$mail->Send();

// send SMS?

$sql = "SELECT sms_remaining FROM SMS WHERE tradesman_id = '".$job_rs['tradesman_id']."'";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
$sms_rs=mysql_fetch_assoc($query);

if($sms_rs['sms_remaining'] > 0 && $user_rs['mobile_verified'] == '1' && $user_rs['accepted_quotes'] == '1'){

// SEND SMS

	$mail = new PHPMailer();
	$mail->IsSMTP(); // telling the class to use SMTP
	$mail->Host = "localhost"; // SMTP server
	$mail->From = $settings['sms_email'];
	$mail->FromName = $settings['sms_email'];

	$mail->AddAddress("44".substr($user_rs['mobile'], 1)."@bulksms.co.uk");

	$mail->Subject = $settings['sms_password'];
	$mail->Body = "Congratulations! A user has accepted your quotation on I Want A Tradesman. Go to your account now to view the details.";

	$mail->Send();


// deduct sms

$new_sms_remaining = $sms_rs['sms_remaining'] - 1;

$sql = "UPDATE SMS SET sms_remaining = '".$new_sms_remaining."' WHERE tradesman_id = '".$job_rs['tradesman_id']."'";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());

}


header("Location: /my-account");

}

?>